package com.suyang.syt.util;

import java.util.Collection;
import java.util.List;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;

public class SolrUtil {
	private static SolrClient client = null;

	private SolrUtil() {

	}

	public static synchronized SolrClient getSolrClient() {
		try {
			if (client == null) {
				String solrServerURL = PropertiesUtil.get("systemContext.properties", "solrServer");
				if (EmptyUtil.isNotEmpty(solrServerURL)) {
					client = new HttpSolrClient(solrServerURL);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return client;
	}

	public static void addBeans(Collection<?> beans, boolean isCommit) throws Exception {
		getSolrClient().addBeans(beans);
		if (isCommit) {
			getSolrClient().commit();
		}
	}

	public static void addBean(Object object, boolean isCommit) throws Exception {
		getSolrClient().addBean(object);
		if (isCommit) {
			getSolrClient().commit();
		}
	}

	public static void deleteBean(String id, boolean isCommit) throws Exception {
		getSolrClient().deleteById(id);
		if (isCommit) {
			getSolrClient().commit();
		}
	}

	public static void deleteBeans(List<String> ids, boolean isCommit) throws Exception {
		getSolrClient().deleteById(ids);
		if (isCommit) {
			getSolrClient().commit();
		}
	}

	public static void deleteByQuery(String query, boolean isCommit) throws Exception {
		getSolrClient().deleteByQuery(query);
		if (isCommit) {
			getSolrClient().commit();
		}
	}

	public static void updateBean(Object obj, int commitWithinMs, boolean isCommit) throws Exception {
		getSolrClient().addBean(obj, commitWithinMs);
		if (isCommit) {
			getSolrClient().commit();
		}
	}

	public static void commit() throws Exception {
		getSolrClient().commit();
	}

	public static void optimize() throws Exception {
		getSolrClient().optimize();
	}

	/** 保存更新课程索引信息 */
	// public static SolrFieldTeacher saveIndex(TrainingInstitution pxjg, long
	// tid, Course course, String scopes) {
	// if (course != null && pxjg != null) {
	// SolrFieldTeacher sft = new SolrFieldTeacher();
	// sft.setId(pxjg.getId() + "_" + tid + "_" + course.getId());
	// sft.setAddress(pxjg.getAddress());
	// sft.setLocation(pxjg.getLat() + "," + pxjg.getLng());
	// sft.setName(course.getName());
	// sft.setLogo(AppUtil.getLogo(course.getLogo(), 3));
	// // 此处为课程的分类名称
	// sft.setScopes(scopes);
	// // 此处为课程评分
	// sft.setScore(course.getScore());
	// // 此处为数据状态 0为机构 1为私教 2为教师 3为课程
	// sft.setStatus(3);
	// // 此处为课程预约人数
	// sft.setSubscribeNum(course.getCouserApplicationSubNum());
	// sft.setFreeNum(course.getCouserApplicationNum());
	// sft.setCid(pxjg.getCid());
	// return sft;
	// }
	// return null;
	// }
	//
	// /** 保存更新教师索引信息 */
	// public static SolrFieldTeacher saveIndex(TrainingInstitution pxjg,
	// Teacher teacher, String scopes) {
	// if (teacher != null && pxjg != null) {
	// SolrFieldTeacher sft = new SolrFieldTeacher();
	// sft.setId(pxjg.getId() + "_" + teacher.getId() + "_0");
	// sft.setAddress(pxjg.getAddress());
	// sft.setLocation(pxjg.getLat() + "," + pxjg.getLng());
	// sft.setLogo(AppUtil.getLogo(teacher.getLogo(), 2));
	// sft.setName(teacher.getName());
	// // 教师特长
	// sft.setScopes(scopes);
	// // 此处为教师的评分
	// sft.setScore(teacher.getTeacherScore());
	// // 此处为数据状态 0为机构 1为私教 2为教师 3为课程
	// if (teacher.getTeacherPrivate() == 1) {
	// sft.setStatus(1);
	// } else {
	// sft.setStatus(2);
	// }
	// // 机构教师的预约人数为0,私教的预约人数=机构的预约人数
	// // sft.setSubscribeNum(pxjg.getSubscribeNum());
	// sft.setCid(pxjg.getCid());
	// return sft;
	// }
	// return null;
	// }
	//
	// /** 保存更新机构索引信息 */
	// public static SolrFieldTeacher saveIndex(TrainingInstitution pxjg, String
	// scopes) {
	// if (pxjg != null && pxjg.getStatus() == 2 &&
	// !pxjg.getPtext().equals("私教")) {
	// SolrFieldTeacher sft = new SolrFieldTeacher();
	// sft.setAddress(pxjg.getAddress());
	// sft.setId(pxjg.getId() + "_0_0");
	// sft.setLocation(pxjg.getLat() + "," + pxjg.getLng());
	// sft.setName(pxjg.getText());
	// sft.setScore(pxjg.getScore());
	// // 此处为数据状态 0为机构 1为私教 2为教师 3为课程
	// sft.setStatus(0);
	// sft.setSubscribeNum(pxjg.getSubscribeNum());
	// // 设置Logo信息
	// sft.setLogo(AppUtil.getLogo(pxjg.getLogo(), 1));
	// // 设置教学范围
	// sft.setScopes(scopes);
	// sft.setCid(pxjg.getCid());
	// return sft;
	// }
	// return null;
	// }

}
